import asyncio
import time
from urllib.parse import urlparse


async def get_url(base_url):
    scheme, netloc, url, params, query, fragment = urlparse(base_url)
    reader, writer = await asyncio.open_connection(netloc, 443)
    writer.write("GET {} HTTP/1.1\r\nHost:{}\r\nConnection:close\r\n\r\n".format(base_url, netloc).encode("utf8"))
    all_lines = []
    async for raw_line in reader:
        data = raw_line.decode()
        all_lines.append(data)
    html = '\n'.join(all_lines)
    return html


async def main():
    tasks = []
    for i in range(10):
        url = 'https://movie.douban.com/top250?start={}&filter='.format(i * 25)
        tasks.append(asyncio.ensure_future(get_url(url)))
    for task in tasks:
        result = await task
        print(result)


if __name__ == '__main__':

    # for i in range(10):
    #     url = 'https://movie.douban.com/top250?start={}&filter='.format(i * 25)
    #     print(url)

    start = time.time()
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
    print('the last time:{}'.format(time.time() - start))
